package vip.xiaonuo.smzq.modular.kcl.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.beanutils.BeanUtils;
import vip.xiaonuo.smzq.modular.kcl.entity.Zhhs;
import vip.xiaonuo.smzq.modular.kcl.entity.vo.ZhhsDetails;
import vip.xiaonuo.smzq.modular.kcl.mapper.ZhhsMapper;
import vip.xiaonuo.smzq.modular.kcl.service.IZhhsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import vip.xiaonuo.common.page.CommonPageRequest;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * 综合回收 服务实现类
 *
 * @author eomer
 * @since 2024-02-22
 */
@Service
public class ZhhsServiceImpl extends ServiceImpl<ZhhsMapper, Zhhs> implements IZhhsService {

    @Resource
    private ZhhsMapper zhhsMapper;

    @Override
    public Page<Zhhs> page(Zhhs zhhs) {
        QueryWrapper<Zhhs> queryWrapper = new QueryWrapper<>();
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Override
    public List<ZhhsDetails> getZhhsList(String kqbm, Integer djflbm, Integer nd) {
        List<ZhhsDetails> zhhsDetails = new ArrayList<>();

        QueryWrapper<Zhhs> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("kqbh", kqbm).eq("djflbh", djflbm).eq("nd", nd);
        List<Zhhs> zhhsDetailList = zhhsMapper.selectList(queryWrapper);
        if (zhhsDetailList != null && zhhsDetailList.size() > 0){

            for (Zhhs zhhs : zhhsDetailList) {
                ZhhsDetails zhhsDetail=new ZhhsDetails();
                try {
                    BeanUtils.copyProperties(zhhsDetail, zhhs);
                    String kcmc = zhhs.getKcmc();
                    zhhsDetail.setKcmcDetail(kcmc.substring(0,kcmc.indexOf(" ")));
                    zhhsDetail.setKcdmDetail(kcmc.substring(kcmc.indexOf(" ")+1,kcmc.length()));
                    zhhsDetails.add(zhhsDetail);

                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }


            }
        }
        return zhhsDetails;
    }

    @Override
    public int addList(List<ZhhsDetails> zhhsList,String kqbm, Integer djflbm, Integer nd) {
        int count = 0;
        QueryWrapper<Zhhs> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("kqbh", kqbm).eq("djflbh", djflbm).eq("nd", nd);
        zhhsMapper.delete(queryWrapper);
        for (ZhhsDetails zhhsDetail : zhhsList) {
            Zhhs zhhs = new Zhhs();
            try {
                BeanUtils.copyProperties(zhhs,zhhsDetail);
                count+=  zhhsMapper.insert(zhhs);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return count;
    }

    @Override
    public List<Zhhs> selectList(QueryWrapper<Zhhs> queryWrapperZhhs) {
        return zhhsMapper.selectList(queryWrapperZhhs);
    }
}
